import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 用户信息
  const userInfo = ref(null)
  
  // 仓库编码
  const warehouseCode = ref('')
  
  // 仓库信息
  const warehouseInfo = ref(null)
  
  // 设置用户信息
  const setUserInfo = (info) => {
    userInfo.value = info
  }
  
  // 获取用户信息
  const getUserInfo = () => {
    return userInfo.value
  }

  // 设置仓库编码
  const setWarehouseCode = (code) => {
    warehouseCode.value = code
    // 同时存储到本地
    uni.setStorageSync('cdx_merchant_warehouseCode', code)
  }
  
  // 获取仓库编码
  const getWarehouseCode = () => {
    return warehouseCode.value
  }

  // 设置仓库信息
  const setWarehouseInfo = (info) => {
    warehouseInfo.value = info
    // 同时存储到本地
    uni.setStorageSync('cdx_merchant_warehouseInfo', JSON.stringify(info))
  }
  
  // 获取仓库信息
  const getWarehouseInfo = () => {
    return warehouseInfo.value
  }

  // 登出方法
  const logout = () => {
    userInfo.value = null
    warehouseCode.value = ''
    warehouseInfo.value = null
    // 清除本地存储
    uni.removeStorageSync('cdx_merchant_userInfo')
    uni.removeStorageSync('cdx_merchant_warehouseCode')
    uni.removeStorageSync('cdx_merchant_warehouseInfo')
  }
  

  
  return {
    // 状态
    warehouseCode,
    warehouseInfo,
    // 方法
    getUserInfo,
    setUserInfo,
    setWarehouseCode,
    getWarehouseCode,
    setWarehouseInfo,
    getWarehouseInfo,
    logout,
  }
})
